﻿package com.gengine.resource
{
    import com.gengine.core.call.*;
    import com.gengine.debug.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.events.*;
    import flash.events.*;
    import flash.utils.*;

    public class LoaderHelp extends Object
    {
        private static var _dicResCallBack:Dictionary = new Dictionary();
        private static var _loadCaller:Caller = new Caller();
        private static var _aryResLoaded:Array = new Array();
        private static var _aryResLoading:Array = new Array();
        private static var _getPath:Function;
        private static var _isAddListener:Boolean = false;

        public function LoaderHelp()
        {
            return;
        }// end function

        private static function isResLoaded(param1:String) : Boolean
        {
            return _aryResLoaded.indexOf(param1) > -1;
        }// end function

        private static function isResLoading(param1:String) : Boolean
        {
            return _aryResLoading.indexOf(param1) > -1;
        }// end function

        private static function addListener() : void
        {
            if (!_isAddListener)
            {
                GlobalClass.libaray.addEventListener(LibraryEvent.SINGLELOAD_COMPLETE, onlibraryCompleteHandler);
                _isAddListener = true;
            }
            return;
        }// end function

        public static function addResCallBack(param1:String, param2:Function = null, param3:String = "") : void
        {
            var path:String;
            var resName:* = param1;
            var callBack:* = param2;
            var resComplPath:* = param3;
            var onErrorEvent:* = function (event:ErrorEvent) : void
            {
                Log.debug("LoderHelp找不到资源：" + resName);
                return;
            }// end function
            ;
            addListener();
            resName = GImageBitmap.getUrl(resName);
            if (isResLoaded(resName))
            {
                if (callBack is Function)
                {
                    callBack.call();
                }
            }
            else if (isResLoading(resName))
            {
                _loadCaller.addCall(resName, callBack);
            }
            else
            {
                _loadCaller.addCall(resName, callBack);
                _aryResLoading.push(resName);
                path = resComplPath;
                if (path == "" || path == null)
                {
                    path = getPath(resName);
                }
                if (path)
                {
                    GlobalClass.libaray.loadSWF(path, resName);
                }
            }
            return;
        }// end function

        private static function onlibraryCompleteHandler(event:LibraryEvent) : void
        {
            var _loc_2:* = String(event.data);
            _aryResLoaded.push(_loc_2);
            if (isResLoading(_loc_2))
            {
                _aryResLoading.splice(_aryResLoading.indexOf(_loc_2), 1);
            }
            _loadCaller.call(_loc_2);
            _loadCaller.removeCallByType(_loc_2);
            return;
        }// end function

        static function getPath(param1:String) : String
        {
            if (_getPath == null)
            {
                return param1;
            }
            return _getPath.call(null, param1);
        }// end function

        public static function setGetPath(param1:Function) : void
        {
            _getPath = param1;
            return;
        }// end function

        public static function setBitmapdata(param1:String, param2:GImageBitmap, param3:Number = 0, param4:Number = 0, param5:Number = -1, param6:Number = -1, param7:Function = null) : void
        {
            if (param7 != null)
            {
                param2.onLoadCompl = param7;
            }
            param2.imgUrl = param1;
            if (param3 != 0)
            {
                param2.x = param3;
            }
            if (param4 != 0)
            {
                param2.y = param4;
            }
            return;
        }// end function

        public static function removeResEvent(param1:String, param2:Function) : void
        {
            param1 = GImageBitmap.getUrl(param1);
            _loadCaller.removeCall(param1, param2);
            return;
        }// end function

    }
}
